/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { HvigorLogger } from '@ohos/hvigor'

const PREFIX = '[TSerialization]'

/**
 * TSerialization 日志工具类
 * 自动添加项目前缀和模块信息，简化日志输出
 */
export class Logger {
  /** 当前模块上下文 */
  private static currentModule: string = ''

  /**
   * 设置当前模块上下文
   * @param moduleName 模块名称
   */
  static setModuleContext(moduleName: string): void {
    this.currentModule = moduleName
  }

  /**
   * 清除当前模块上下文
   */
  static clearModuleContext(): void {
    this.currentModule = ''
  }

  /**
   * 获取当前模块上下文
   */
  static getCurrentModule(): string {
    return this.currentModule
  }

  /**
   * 格式化日志消息
   * @param msg 原始消息
   * @returns 格式化后的消息
   */
  private static formatMessage(msg: string): string {
    const modulePrefix = this.currentModule ? `[${this.currentModule}]` : ''
    return `${PREFIX}${modulePrefix} ${msg}`
  }

  /**
   * 输出错误日志
   * @param msg 错误消息
   * @param args 额外参数
   */
  static error(msg: string, ...args: unknown[]): void {
    const formattedMsg = this.formatMessage(msg)
    if (args.length > 0) {
      HvigorLogger.getLogger().error(formattedMsg, ...args)
    } else {
      HvigorLogger.getLogger().error(formattedMsg)
    }
  }

  /**
   * 输出信息日志
   * @param msg 信息消息
   * @param args 额外参数
   */
  static info(msg: string, ...args: unknown[]): void {
    const formattedMsg = this.formatMessage(msg)
    if (args.length > 0) {
      HvigorLogger.getLogger().info(formattedMsg, ...args)
    } else {
      HvigorLogger.getLogger().info(formattedMsg)
    }
  }

  /**
   * 输出警告日志
   * @param msg 警告消息
   * @param args 额外参数
   */
  static warn(msg: string, ...args: unknown[]): void {
    const formattedMsg = this.formatMessage(msg)
    if (args.length > 0) {
      HvigorLogger.getLogger().warn(formattedMsg, ...args)
    } else {
      HvigorLogger.getLogger().warn(formattedMsg)
    }
  }

  /**
   * 输出调试日志
   * @param msg 调试消息
   * @param args 额外参数
   */
  static debug(msg: string, ...args: unknown[]): void {
    const formattedMsg = this.formatMessage(msg)
    if (args.length > 0) {
      HvigorLogger.getLogger().debug(formattedMsg, ...args)
    } else {
      HvigorLogger.getLogger().debug(formattedMsg)
    }
  }

  /**
   * 在指定模块上下文中执行函数，执行完毕后自动清理上下文
   * @param moduleName 模块名称
   * @param fn 要执行的函数
   * @returns 函数执行结果
   */
  static withModuleContext<T>(moduleName: string, fn: () => T): T {
    const previousModule = this.currentModule
    try {
      this.setModuleContext(moduleName)
      return fn()
    } finally {
      // 恢复之前的模块上下文
      this.currentModule = previousModule
    }
  }

  /**
   * 在指定模块上下文中执行异步函数，执行完毕后自动清理上下文
   * @param moduleName 模块名称
   * @param fn 要执行的异步函数
   * @returns Promise
   */
  static async withModuleContextAsync<T>(moduleName: string, fn: () => Promise<T>): Promise<T> {
    const previousModule = this.currentModule
    try {
      this.setModuleContext(moduleName)
      return await fn()
    } finally {
      // 恢复之前的模块上下文
      this.currentModule = previousModule
    }
  }
}
